#!/bin/bash
#
# Frobisher: Build virtualization packages.
#
# Copyright (C) 2013 IBM, Inc.
# Author: Wang Sen <wangsen@linux.vnet.ibm.com>
#
# To build a frobisher package(QEMU, Libvirt, Libvirt-cim, etc.), you need to 
# run this script as follows:
# 
# build_virt.sh -n name -r release -s update_number [-u] [-h] [-t] -f *.spec
# -n: The package name you want to build, e.g.qemu.
# -h: Print the help and exit.
# -u: Update the repo of ltcphx.austin.ibm.com.
# -r: Release number. e.g. 3 of ppc arch means pbuild3.
# -s: Update number. Same to %release in the spec file.
# -t: Make test build.
# -f: spec file

set -e
set -u

# Variables

linux_repo="linux-3.10.23"

name=""
# Package name, specified by -n. (required)

release=""
# Frobisher release, specified by -r. (required)

repo="/scratch/isos/frobisher/extra-packages"
# The repository to be updated. Will change according to the options.

update_repo="1"
# Update repo or not. Determined by -u.

test_flag="1"

srpm_path="."
# The path of SRPM generated by rpmbuild.

task_id=""
# The task id of koji.

rpm_path_prefix="/mnt/koji/scratch/${USER}/task_"
rpm_path=""
# rpm_path = ${rpm_path_prefix}${task_id}

sub_release=""
# Update number, specified by -s (required).

spec_file=""

log() { printf "`date +%Y%m%d%H%M.%N`: %s\n" "$*"; }
quit() { log "$*" ; exit 1; }

usage () {
	cat << EOF
Usage: 
build_virt.sh -n name -r release [-u] [-h] -s update_number [-t] -f *.spec
	-t: Make test build.
	-n: The package name you want to build, e.g.QEMU.
	-h: Print the help and exit. 
	-u: Update the repo of ltcphx.austin.ibm.com or not. 
	-r: Release number. e.g. 3 of ppc arch means pbuild3.
   	-s: Update number. Same to %release in the spec file.
	-f: spec file.
EOF
}

if test "$#" == "0"
then
	usage; exit 0;
fi

# Parsing the arguments.
while getopts r:n:hus:tf: tmp
do
	case $tmp in
		r) release=($OPTARG);;
		n) name=($OPTARG);;
		s) sub_release=($OPTARG);;
		f) spec_file=($OPTARG);;
		u) update_repo="0";;
		t) test_flag="0";;
		h) usage; exit 0;;
		'?') quit "invalid option `echo $OPTARG`";;
		:) quit "missing argument to `echo $OPTARG`";;
		*) quit "argument error";;
	esac
done

if test -z ${name}; then
	quit "missing argument to -n"
fi

if test -z ${release}; then
	quit "missing argument to -r"
fi

if test -z ${sub_release}; then
	quit "missing argument to -s"
fi

# Variables assignment.

if test ${test_flag} == "0" ; then
	if test $name == "kernel";then
		git_url="git://9.3.189.26/frobisher/test_repo/${linux_repo}-test.git \
				 ${linux_repo}"
	else
		git_url="git://9.3.189.26/frobisher/test_repo/${name}-test.git ${name}"
	fi
else
	if test ${name} == "kernel";then
		git_url="git://9.3.189.26/frobisher/${linux_repo}.git"
	else
		git_url="git://9.3.189.26/frobisher/${name}.git"
	fi
fi

# if update number is not 0, we will build update packages.
git_branch="powerkvm" #to-do
if test $sub_release != "0" ; then
	repo="${repo}/../PowerKVM/updates/0.${release}"
else
	repo="${repo}/ppc64/build${release}"
fi

# Build srpm according to the spec file in git repo.
build_srpm() {
	if test -z $spec_file; then
		if test -d "./${name}" ; then
			rm -rf $name
		fi
		git clone ${git_url}
		cd ${name}
		git checkout --track remotes/origin/${git_branch}
	fi
	if test ! -f ${spec_file} ; then
		quit "Error: ${spec_file} do not exists"
	fi
	if test ${test_flag} == "0" ; then
		if test ${name} == "kernel"; then
			sed -i s/"${linux_repo}.git"/"test_repo\/${linux_repo}-test.git"/g \
				${spec_file}
		else
			sed -i s/"${name}.git"/"test_repo\/${name}-test.git"/g ${spec_file}
		fi
	fi
	srpm_path=`rpmbuild -bs ${spec_file} | cut -d ':' -f 2`
	srpm_path=`echo $srpm_path` # get rid of the space
	if test -z $spec_file; then
		cd ..
		rm -rf $name
	fi
}

# Build RPMs. rpm_path indicates path to output RPMs.
build_rpm() {
	if test -z $srpm_path ; then
		quit "Error:${name}:Build srpm error"
	fi
	task_id=`koji build --scratch --arch-override=ppc64 pkvm2_1 ${srpm_path} | grep "^Created.task" | cut -d ':' -f 2`
	task_id=`echo $task_id`	
	if test -z $task_id ; then
		log "Error:${name}:Build rpm error: task id is null" | mail -s "Build $name failed" -t "wangsen@linux.vnet.ibm.com" \
		-a "From: frobisher@koji"
		exit 1
	fi
	rpm_path="${rpm_path_prefix}${task_id}"
	if test ! -d $rpm_path ; then
		log "Error:${name}:visit http://mcpkjhub1.austin.ibm.com/koji/taskinfo?taskID=${task_id} for more details" \
		| mail -s "Build $name failed" -t "wangsen@linux.vnet.ibm.com" -a "From: frobisher@koji"
		exit 1
	fi
}

# Copy output RPMs and logs to ltcphx, login bc2cn9 to createrepo.
create_repo() {
	echo "Updating the repository on ltcphx..."
	if test -z $rpm_path ; then
		quit "Error:${name}:Update repo: RPMs not exist"
	fi
	echo "Copy RPMs to frobisher@ltcphx.austin.ibm.com:${repo}."
	scp ${rpm_path}/*.rpm frobisher@9.3.189.26:$repo
	if test $? -ne 0 ; then
		quit "Copy file error"
	fi
	cp -r ${rpm_path}/logs/ppc64 "log_${name}_${release}_${sub_release}"
	scp -r log_${name}_${release}_${sub_release} frobisher@9.3.189.26:$repo
	if test $? -ne 0 ; then
		quit "Copy file error"
	fi
	rm -rf ./log_${name}_${release}_${sub_release}
ssh frobisher@9.3.189.26 << "ENDSSH"
./create_repo.sh
ENDSSH
	if test $? -eq 0 ; then
		return 0
	else
		return 1
	fi
}

build_srpm
build_rpm

if test ${test_flag} == "0" ; then
	echo "Succ:${name}:Test Successfully"
	exit 0
fi

if test ${update_repo} == "0" ; then
	create_repo
fi
